/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package logica;

import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Meme
 */
public class RatingDriver {
    private int counter;
    
    public RatingDriver() {
        counter = 1;
    } 
      
    /**
     * agrega un comentario a la base de datos
     */
    public int putCommentOnDB(String comentario, int puntaje, int restaurante, int usuario){
        String SQLStatement = "insert into calificacion (codigo, comentario, puntaje, Restaurante_codigo, Usuario_codigo)"+
                               "values ("+counter+", \'"+comentario+"\', "+puntaje+","+restaurante+","+usuario+")";

        java.sql.Connection conection = datos.RDMS.GetConnection();
        if (datos.RDMS.actualizar(SQLStatement, conection) != 0){
            counter++;
            return 1;
        }
        
        return 0;
    }
    
    public void obtainRatings(StructureManager manager) {
        ItemRating tmprating = null;

        java.sql.Connection conection = datos.RDMS.GetConnection();
        java.sql.ResultSet result = datos.RDMS.consulta("select * from Calificacion", conection);
        if (result != null) {
            try {
                counter = 1;
                while (result.next()) {
                    tmprating = new ItemRating();
                    tmprating.setComment(result.getString(3));
                    tmprating.setScore(result.getInt(2));
                    tmprating.setResCode(result.getInt(5));
                    tmprating.setUsrCode(result.getInt(4));
                    manager.addRatingToRestaurant(result.getInt(5)-1, tmprating);
                    counter++;
                }
                result.close();
                conection.close();
            } catch (SQLException ex) {
                Logger.getLogger(this.toString()).log(Level.SEVERE, null, ex);
            }
        }
    }

/**
 * @return lista de los nombres de los restaurantes
 */
    public StructureManager getRestaurants(){
        StructureManager smanager = new StructureManager();

        java.sql.Connection conection = datos.RDMS.GetConnection();
        java.sql.ResultSet result = datos.RDMS.consulta("select nombre from restaurante", conection);
        if (result != null) {
            try {
                while (result.next()) {
                    smanager.addRestaurant(result.getString(1));
                }
                
                result.close();
                conection.close();
            } catch (SQLException ex) {
                Logger.getLogger(RatingDriver.class.getName()).log(Level.SEVERE, null, ex);
                return null;
            }
        }
        return smanager;
    }    
}
